package siam.core;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Class which allow to extract resources from jar
 * @author Motassim Saad
 *
 */
public class ExtractFiles {
        /**
         * List files to extract
         */
        private static Collection<String> listeFilesToExtract;
        /**
         * root directory destination
         */
        private static final String rootDirectory = "tmp";
        /**
         * unique instance of extractor
         */
        private static ExtractFiles instance;
        
        /**
         *Init the list file to extract
         */
        private ExtractFiles() {
                listeFilesToExtract = new ArrayList<String>();
                listeFilesToExtract.add("1");
                listeFilesToExtract.add("2");
                listeFilesToExtract.add("Rock_Icon.png");
                listeFilesToExtract.add("bottom.png");
                listeFilesToExtract.add("top.png");
                listeFilesToExtract.add("left.png");
                listeFilesToExtract.add("right.png");
                
                
        }
        /**
         * initialisation the unique instance
         */
        public static void init() {
                if(instance == null)
                        instance = new ExtractFiles();
        }
        
        /**
         * Return the unique instance of ExtractFiles
         * @return ExtractFiles
         */
        public static ExtractFiles getInstance() {
                return instance;
        }
        
        /**
         * Extract the script in tempory file
         */
        public File extractFile(String fileName) {
                File script = null;
                
                try {
                        // Jar path
                        String path = ExtractFiles.class.getProtectionDomain().getCodeSource().getLocation().getPath();
                        String decodedPath = URLDecoder.decode(path, "UTF-8");

                        @SuppressWarnings("resource")
						JarFile jarFile = new JarFile(decodedPath); 
                        JarEntry je = jarFile.getJarEntry(fileName);
                        script = new File(rootDirectory+"/"+fileName);

                        InputStream is = jarFile.getInputStream(je);
                        FileOutputStream fos = new FileOutputStream(script);
                        while (is.available() > 0) { 
                                fos.write(is.read());
                        }
                        fos.close();
                        is.close();
                }
                catch (FileNotFoundException e) {} 
                catch (UnsupportedEncodingException e) {} 
                catch (IOException e) {}
                
                return script;
        }
        /**
         * Extract the file from jar to specifc destination
         */
        public void extractFiles() {
                File root = new File(rootDirectory);
                root.mkdir();
                root.deleteOnExit();
                for (String path : listeFilesToExtract) {
                		File f = extractFile(path);
                        f.deleteOnExit();
                }
        }
        
}